package com.chuyu.jzwboa.widget;

import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.chuyu.jzwboa.MainActivity;
import com.chuyu.jzwboa.R;
import com.chuyu.jzwboa.adapter.main.TabAdapter;
import com.chuyu.jzwboa.view.iview.main.IMainView;

/**
 * @author 杨仕俊
 * @description 主界面的viewpager 切换
 * Created by wo on 2017/12/15.
 */

public class MainVpagerView {
    private RadioButton tabMain;

    private RadioButton tabMsg;

    private RadioButton tabSearch;

    private RadioButton tabMe;

    private RadioGroup mainTabBottom;

    private NoScrollViewPager mainViewpager;

    private MainActivity context;

    private Fragment[] fragment;

    private TextView mainTitelText;

    public MainVpagerView(MainActivity context,
                          RadioButton tabMain, RadioButton tabMsg, RadioButton tabSearch, RadioButton tabMe
            , RadioGroup mainTabBottom, NoScrollViewPager mainViewpager, Fragment[] fragment, TextView mainTitelText) {
        this.context = context;
        this.tabMain = tabMain;
        this.tabMsg = tabMsg;
        this.tabSearch = tabSearch;
        this.tabMe = tabMe;
        this.mainTabBottom = mainTabBottom;
        this.mainViewpager = mainViewpager;
        this.fragment = fragment;

        this.mainTitelText=mainTitelText;
    }

    /**
     * 初始化底部标签页
     */
    public void setTabs() {
        //RadioGroup的监听
        mainTabBottom.setOnCheckedChangeListener(listener);
    }


    /**
     * 设置Fragment Viewpager
     */
    public void initFragmentViewPager() {
        //设置viewpager
        TabAdapter<Fragment> tabAdapter = new TabAdapter<>(context.getSupportFragmentManager(), fragment);
        setPager(tabAdapter, new IMainView.OnPageSelectedListener() {

            @Override
            public void onPageSelected(int position) {
                pageChangeListener(position);
            }
        });
    }

    /**
     * 初始化viewpager
     */
    private void setPager(TabAdapter<Fragment> adapter, final  IMainView.OnPageSelectedListener listener ){
        mainViewpager.setOffscreenPageLimit(3);
        mainViewpager.setNoScroll(true);
        mainViewpager.setAdapter(adapter);
        //ViewPager滑动的监听
        mainViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                listener.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    /**
     * RadioGroup的监听
     */
    private RadioGroup.OnCheckedChangeListener listener = new RadioGroup.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
            //fragment变动
            tabChangeListener(i);
        }
    };

    /**
     * RadioButton的点击事件
     * 联动引起viewpager切换
     *
     * @param checkedId
     */
    private void tabChangeListener(int checkedId) {
        switch (checkedId) {
            case R.id.tab_main:
                mainViewpager.setCurrentItem(0, false);
                break;
            case R.id.tab_msg:
                mainViewpager.setCurrentItem(1, false);
                break;
            case R.id.tab_search:
                mainViewpager.setCurrentItem(2, false);
                break;
            case R.id.tab_me:
                mainViewpager.setCurrentItem(3, false);
                break;
            default:
                break;
        }
    }

    /**
     * ViewPager的页面切换
     * 联动引起RadioButton的选择
     *
     * @param page
     */
    private void pageChangeListener(int page) {
        switch (page) {
            case 0:
                tabMain.setChecked(true);
                mainTitelText.setText("首页");
                context.setFragementIndex(0);
                break;
            case 1:
                tabMsg.setChecked(true);
                mainTitelText.setText("消息");
                context.setFragementIndex(1);
                break;
            case 2:
                tabSearch.setChecked(true);
                mainTitelText.setText("搜索");
                context.setFragementIndex(2);
                break;
            case 3:
                tabMe.setChecked(true);
                mainTitelText.setText("我的");
                context.setFragementIndex(3);
                break;
            default:
                break;
        }
    }
}
